
Partial Class FirstSample
    Inherits System.Web.UI.Page

    Protected Sub btnConnect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Me.WebPartManager1.DisplayMode = WebPartManager.ConnectDisplayMode
    End Sub
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim prov As Chapter10CustomControlsVB.ProviderPart
        Dim cons As Chapter10CustomControlsVB.ConsumerPart

        prov = CType(Me.znProvider.WebParts("ProviderPart1"), Chapter10CustomControlsVB.ProviderPart)
        cons = CType(Me.znConsumer.WebParts("ConsumerPart1"), Chapter10CustomControlsVB.ConsumerPart)

        Dim cncp As ConsumerConnectionPointCollection
        Dim prcp As ProviderConnectionPointCollection

        prcp = Me.WebPartManager1.GetProviderConnectionPoints(prov)
        cncp = Me.WebPartManager1.GetConsumerConnectionPoints(cons)

        If Me.WebPartManager1.CanConnectWebParts(prov, prcp(0), cons, cncp(0)) Then
            Me.WebPartManager1.ConnectWebParts(prov, prcp(0), cons, cncp(0))
        End If

    End Sub

    Protected Sub btnTransform_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTransform.Click
        Dim prov As Chapter10CustomControlsVB.ProviderPart
        Dim prcp As ProviderConnectionPointCollection
        Dim cons As Chapter10CustomControlsVB.ConsumerPart
        Dim cncp As ConsumerConnectionPointCollection
        Dim trs As New Chapter10CustomControlsVB.BookToAuthorTransform
        Dim wpt As WebParts.WebPartTracker

        prov = CType(Me.znProvider.WebParts("FindBook1"), Chapter10CustomControlsVB.ProviderPart)
        cons = CType(Me.znConsumer.WebParts("DisplayAuthor1"), _
                                               Chapter10CustomControlsVB.ConsumerPart)

        prcp = Me.WebPartManager1.GetProviderConnectionPoints(prov)
        cncp = Me.WebPartManager1.GetConsumerConnectionPoints(cons)
        wpt = New WebParts.WebPartTracker(prov, prcp(0))
        If wpt.IsCircularConnection = False Then
            If Me.WebPartManager1.CanConnectWebParts(prov, prcp(0), cons, cncp(0), _
                                                                                trs) Then
                Me.WebPartManager1.ConnectWebParts(prov, prcp(0), cons, cncp(0), trs)
            End If
        End If

    End Sub

    Protected Sub btnTransient_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTransient.Click
        Dim fbk As Chapter10CustomControlsVB.ProviderPart
        Dim dsk As Chapter10CustomControlsVB.ConsumerPart
        Dim ibk As Chapter10CustomControlsVB.IBookInfo

        fbk = CType(Me.znConsumer.WebParts("FindBook1"), Chapter10CustomControlsVB.ProviderPart)
        dsk = CType(Me.znProvider.WebParts("DisplayBook1"), Chapter10CustomControlsVB.ConsumerPart)

        ibk = fbk.IBookInfoProvider()
        dsk.IBookInfoConsumer(ibk)

    End Sub

    Protected Sub btnBreak_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBreak.Click
        Dim prov As Chapter10CustomControlsVB.ProviderPart
        Dim cons As Chapter10CustomControlsVB.ConsumerPart
        Dim prcp As ProviderConnectionPointCollection
        Dim cncp As ConsumerConnectionPointCollection

        Dim idx As Integer

        prov = CType(Me.znProvider.WebParts("FindBook1"), Chapter10CustomControlsVB.ProviderPart)
        cons = CType(Me.znConsumer.WebParts("DisplayAuthor1"), _
                                               Chapter10CustomControlsVB.ConsumerPart)

        prcp = Me.WebPartManager1.GetProviderConnectionPoints(prov)
        cncp = Me.WebPartManager1.GetConsumerConnectionPoints(cons)

        If Me.WebPartManager1.CanConnectWebParts(prov, prcp(0), cons, cncp(0)) Then
            Me.WebPartManager1.ConnectWebParts(prov, prcp(0), cons, cncp(0))
            idx = Me.WebPartManager1.Connections.Count - 1
            Me.WebPartManager1.Connections(idx).ID = "NewConnection"
        End If
        Dim cn As System.Web.UI.WebControls.WebParts.WebPartConnection

        cn = Me.WebPartManager1.Connections("NewConnection")
        Me.WebPartManager1.DisconnectWebParts(cn)

    End Sub
End Class
